package com.longjianghuasheng.logistics.business.address

import android.os.Bundle
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.view.OptionsPickerView
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseTitleFragment
import com.longjianghuasheng.logistics.databinding.FragmentAddressBinding
import com.longjianghuasheng.logistics.ext.observer

class AddressFragment : BaseTitleFragment<AddressViewModel, FragmentAddressBinding>() {

    override fun layoutId(): Int = R.layout.fragment_address

    var type = 1
    var address = ""
    var p = 0
    var c = 0

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        mDatabind.run {
            stvMessageNotify.setSwitchCheckedChangeListener { buttonView, isChecked ->
                mViewModel.is_default = if (isChecked) 1 else 0
            }

            tvArea.setOnClickListener {
                type = 1
                address = ""
                mViewModel.area(0)
            }

        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.resultArea.observer(this) {
            val pvOptions: OptionsPickerView<String> = OptionsPickerBuilder(
                context
            ) { options1, option2, options3, v ->
                when(type){
                    1->{
                        p = it.get(options1).id!!
                        type++
                        address+=it.get(options1).classname
                        mViewModel.area(it.get(options1).id!!)
                    }
                    2->{
                        c = it.get(options1).id!!
                        type++
                        address+=it.get(options1).classname
                        mViewModel.area(it.get(options1).id!!)
                    }
                    3->{
                        mViewModel.province = p
                        mViewModel.city = c
                        mViewModel.area = it.get(options1).id!!
                        address += it.get(options1).classname
                        mDatabind.tvArea.centerTextView.setText(address)
                    }
                }

            }.build()
            pvOptions.setPicker(it.map { it.classname })
            pvOptions.show()
        }

        mViewModel.notify.observe(this){
            navigateUp()
        }

        mViewModel.result1.observe(this){
            mDatabind.run {
                try {
                    mViewModel.province = it.province?.toInt()?:0
                    mViewModel.city = it.city?.toInt()?:0
                    mViewModel.area = it.area?.toInt()?:0
                    tvName.centerEditText?.setText(it.names)
                    tvPhone.centerEditText?.setText(it.mobile)
                    tvAddress.centerEditText?.setText(it.address)
                    mViewModel.is_default = it.is_default
                    stvMessageNotify.setSwitchIsChecked(it.is_default==1)
                } catch (e: Exception) {
                }
            }
        }

    }

    override fun initData() {
        super.initData()
        arguments?.run {
           if (getInt(KEY_TYPE)==0){
               initTitleBar("新建地址")
           }else{
               initTitleBar("修改地址")
               mViewModel.id = getInt(KEY_TYPE)
               mDatabind.tvArea.centerTextView?.setText(getString(PROVINCE)+getString(CITY)+getString(AREA))
               mViewModel.requestData(getInt(KEY_TYPE))
           }
        }
        mDatabind.run {
            btSubmit.setOnClickListener {
                mViewModel.run {
                    name = tvName.centerEditValue
                    mobile = tvPhone.centerEditValue
                    address = tvAddress.centerEditValue
                    addAddress()
                }
            }
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
    }

    companion object {
        const val KEY_TYPE = "type"
        const val PROVINCE = "province"
        const val CITY = "city"
        const val AREA = "area"

    }
}